//
//  ViewController.m
//  单选、多选
//
//  Created by lili on 16/8/23.
//  Copyright © 2016年 陈浩. All rights reserved.
//

#import "ViewController.h"
#import "SelectedCell.h"
#define UIScreenW [UIScreen mainScreen].bounds.size.width
#define UIScreenH [UIScreen mainScreen].bounds.size.height
#define RGB(a,b,c)  [UIColor colorWithRed:a/255.0 green:b/255.0 blue:c/255.0 alpha:1]
#define footH 44
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) NSMutableArray *dataArray;
@property (nonatomic ,strong) NSMutableArray *selectArr;
@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic ,strong) UIView *footView;     //  底部菜单
@property (nonatomic ,assign) BOOL isSelected;     // 是否全选
@property (nonatomic ,assign) BOOL isAllSelected;
@property (nonatomic ,strong) UIButton *allSelectBtn;
@end

@implementation ViewController
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray arrayWithArray:@[@"数据1",@"数据2",@"数据3",@"数据4",@"数据5",@"数据6",@"数据7",@"数据8",@"数据9",@"数据10"]];
    }
    return _dataArray;
}

-(NSMutableArray *)selectArr{
    if (!_selectArr) {
        _selectArr = [NSMutableArray array];
    }
    return _selectArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"多选";
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, UIScreenW, UIScreenH - footH)];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_tableView];
    
    _footView = [[UIView alloc] initWithFrame:CGRectMake(0, UIScreenH - footH, UIScreenW, footH)];
    _footView.backgroundColor = RGB(244, 244, 244);
    [self.view addSubview:_footView];
    
    _allSelectBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, 0, footH, footH)];
    [_allSelectBtn setImage:[UIImage imageNamed:@"check_gray"] forState:UIControlStateNormal];
    [_allSelectBtn setImage:[UIImage imageNamed:@"checked_all"] forState:UIControlStateSelected];
    [_allSelectBtn addTarget:self action:@selector(allSelectBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_footView addSubview:_allSelectBtn];
    
    UILabel *allSelectLabel = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_allSelectBtn.frame), 0, footH, footH)];
    allSelectLabel.text = @"全选";
    allSelectLabel.textAlignment = NSTextAlignmentCenter;
    [_footView addSubview:allSelectLabel];
    
    
    UIButton *delBtn = [[UIButton alloc] initWithFrame:CGRectMake(UIScreenW -90, 7, 80, 30)];
    [delBtn setTitle:@"删除" forState:UIControlStateNormal];
    [delBtn addTarget:self action:@selector(delBtnClick) forControlEvents:UIControlEventTouchUpInside];
    delBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    delBtn.backgroundColor = RGB(253, 103, 105);
    [_footView addSubview:delBtn];
    
}

-(void)allSelectBtnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    _isAllSelected = btn.selected;
    for (int i = 0 ;i < self.dataArray.count; i++) {
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionTop];
        
    }
    [self.selectArr addObjectsFromArray:self.dataArray];
    if (!btn.selected) {
        [self.selectArr removeAllObjects];
    }
    [self.tableView reloadData];
    
    
}

-(void)delBtnClick{
   
    [self.dataArray removeObjectsInArray:self.selectArr];
    [self.tableView reloadData];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SelectedCell *cell = [SelectedCell cellForTableView:tableView];
     cell.isAllSelected = _isAllSelected;
    cell.textLabel.text = self.dataArray[indexPath.row];
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    SelectedCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selected = !cell.selected;
    cell.isSelected = !cell.isSelected;
    if (cell.selected) {
        [self.selectArr addObject:[self.dataArray objectAtIndex:indexPath.row]];
        if (self.selectArr.count == self.dataArray.count) {
            _allSelectBtn.selected = YES;
        }

    }else{
        [self.selectArr removeObject:[self.dataArray objectAtIndex:indexPath.row]];
        if (self.selectArr.count != self.dataArray.count) {
            self.allSelectBtn.selected = NO;
        }
    }
   
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
